package zxn.circleboard.demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.zxn.utils.UIUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    @BindView(R.id.cbv_cancvs)
    CircleBoardView cbvCancvs;
    @BindView(R.id.rb_paint)
    RadioButton rbPaint;
    @BindView(R.id.rb_led)
    RadioButton rbLed;
    @BindView(R.id.btn_del)
    TextView btnDel;
    @BindView(R.id.btn_ok)
    TextView btnOk;
    @BindView(R.id.rg_bottom)
    RadioGroup rgBottom;
    @BindView(R.id.sv_scroll)
    ScrollView svScroll;
    @BindView(R.id.sb_seekbar)
    SeekBar sbSeekbar;

    private int screenHeight;
    private int mCurrentProgress;
    private int mRangeH;

    public static Bitmap getBitmapByView(ScrollView scrollView) {
        int h = 0;
        Bitmap bitmap = null;
        // 获取scrollview实际高度
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
            /*scrollView.getChildAt(i).setBackgroundColor(
                    Color.parseColor("#ffffff"));*/
        }
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.RGB_565);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        UIUtils.init(this);
        rgBottom.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_paint) {
                    cbvCancvs.setDrawMode(0);
                } else if (checkedId == R.id.rb_led) {
                    cbvCancvs.setDrawMode(1);
                }
            }
        });
        sbSeekbar.setMax(100);
        sbSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //Log.i(TAG, "onProgressChanged: --progress:" + progress+"mCurrentProgress:"+mCurrentProgress);
                if (progress > mCurrentProgress) {

                    int value = (int) Math.ceil((progress - mCurrentProgress) / 100f * mRangeH);
                    Log.i(TAG, "onProgressChanged: --progress:" + progress + "mCurrentProgress:" + mCurrentProgress + "value:" + value + "--mRangeH:" + mRangeH);
                    svScroll.scrollBy(0, value);
                    mCurrentProgress = progress;
                } else {//if (progress < mCurrentProgress)
                    int value = (int) Math.ceil((Math.abs(progress - mCurrentProgress)) / 100f * mRangeH);
                    Log.i(TAG, "onProgressChanged: --progress:" + progress + "mCurrentProgress:" + mCurrentProgress + "value:" + value + "--mRangeH:" + mRangeH);
                    svScroll.scrollBy(0, -value);
                    mCurrentProgress = progress;
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Log.i(TAG, "onStartTrackingTouch: --progress:" + seekBar.getProgress());
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mCurrentProgress = seekBar.getProgress();
                Log.i(TAG, "onStopTrackingTouch: --progress:" + mCurrentProgress);
            }
        });

        screenHeight = UIUtils.getScreenHeight(this);
        int showH = screenHeight - UIUtils.getDimensionPixelSize(R.dimen.dp_80);
        int totalHeight = svScroll.getChildAt(0).getHeight();
        Log.i(TAG, "onCreate: showH:" + showH + "--screenHeight:" + screenHeight + "--totalHeight:" + totalHeight);
        //showH:2244--screenHeight:2244--totalHeight:0
        svScroll.post(new Runnable() {
            @Override
            public void run() {
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560--rangeH7316svHeight:1791
                int svHeight = svScroll.getHeight();
                int totalHeight = svScroll.getChildAt(0).getHeight();
                //mRangeH = totalHeight - showH;
                mRangeH = totalHeight - svHeight;
                Log.i(TAG, "onCreate: showH:" + showH + "--screenHeight:"
                        + screenHeight + "--totalHeight:" + totalHeight + "--rangeH" + mRangeH + "svHeight:" + svHeight);
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560
                //onCreate: showH:2004--screenHeight:2244--totalHeight:9560--rangeH7556
            }
        });
    }

    @OnClick({R.id.btn_del, R.id.btn_ok, R.id.tv_show})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_del:
                cbvCancvs.reset();
                break;
            case R.id.btn_ok:
                finish();
                break;
            case R.id.tv_show:
                toShow();
                Log.i(TAG, "onViewClicked: tv_show");
                scroll();
                break;
        }
    }

    private void toShow() {
        Intent intent = new Intent(this, ShowActivity.class);
        //android.os.TransactionTooLargeException: data parcel size 7085360 bytes
        //Caused by: android.os.TransactionTooLargeException: data parcel size 7085396 bytes

        Bundle bundle = new Bundle();
        //bundle.putParcelable("bp",createBitmap(cbvCancvs));
        //intent.putExtra("bundle",bundle);
        //intent.putExtra("bp", createBitmap(cbvCancvs));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            Bitmap bitmap = getBitmapByView(svScroll);
            bundle.putBinder("bitmap", new ImageBinder(bitmap));
        }
        intent.putExtras(bundle);
        startActivity(intent);
    }

    private Bitmap createBitmap(View view) {
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

    public void scroll() {
        //mScrollView.fullScroll(ScrollView.FOCUS_UP);//滑到顶部
        //svScroll.fullScroll(ScrollView.FOCUS_DOWN);//滑到底部
        svScroll.scrollBy(0, 100);
    }
}


//D:\ZxnProgram\Java\jdk1.8.0_91\bin
//jarsigner -verbose -keystore my.keystore -signedjar tap_signed.apk tap_unsign.apk androiddebugkey
